import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import $store from "../store";
import serve from "../util/request";
import { SET__LOCAL_ITEM } from "../util/utils";
Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/detail/:id',
    name: 'Detail',
    component: () => import('../views/Detail.vue')
  },
  {
    path: '/form',
    name: 'Form',
    component: () => import('../views/Form.vue')
  },
]

const router = new VueRouter({
  routes
})


router.beforeEach(async (to, from, next) => {
  console.log("路由守卫");
  // ...
  await serve
    .post("/login", {
      phone: 13450809860,
      password: 123
    })
    // await axios.post('http://newshopapi.0melon0.cn/api/login', { phone: 13450809860, password: 123 })
    .then(async (res) => {
      console.log(res);
      let token = JSON.stringify({
        data: {
          token: res.result.token
        }
      });
      SET__LOCAL_ITEM("token", token);
      next()
      // token = JSON.parse(GET_LOCAL_ITEM("token"));
      await $store.dispatch("classifyGoods");
      await $store.dispatch("city");
     
    });

})
export default router